﻿
using System;
using AccessControl.Common;
using AccessControl.Common.Delegates;
using AccessControl.Common.Enumerations;
using AccessControl.DataAcess;


namespace AccessControl.Business
{
    public static class PersonEventBuisness
    {
        public static bool CheckPersonByCard(ref string pkPerson, ref bool isFingerRequired, ExternalBuisnessCheck externalBuisnessCheckFunc, int pkEventType, int pkEvent, out string reason)
        {
            reason = "";

            if (string.IsNullOrEmpty(pkPerson)) return false;

            var tblPerson = PersonAccess.SelectByFilter("pkPerson='" + pkPerson.Trim() + "'");
            var tblPersonEvent = PersonEventAccess.SelectByFilter("fkPerson='" + pkPerson.Trim() + "'");
            AccessControlDS.tblPersonEventRow personEvent = null;
            if (tblPerson == null || tblPerson.Rows.Count == 0)
            {
                pkPerson = null;
                reason = "Person not found";
                return false;
            }
            var person = (AccessControlDS.tblPersonRow)tblPerson.Rows[0];

            if (tblPersonEvent != null && tblPersonEvent.Rows.Count > 0)
            {
                personEvent = (AccessControlDS.tblPersonEventRow)tblPersonEvent.Rows[0];
                isFingerRequired = isFingerRequired && (
                    (AccessProcedureConstants)personEvent.AccessProcedure == AccessProcedureConstants.Default
                    || (AccessProcedureConstants)personEvent.AccessProcedure == AccessProcedureConstants.CheckFinger);
            }


            var personAllowed = true;

            //var lastLog = EventLogAccess.GetLastEventDate(pkPerson, pkEvent);
            //if (lastLog!=null && (DateTime)lastLog>DateTime.Now.AddMinutes(-5)){
            //    personAllowed = false;
            //    reason = "تردد مجدد با یک کارت منع شده است!";
            //}else 
            if (personEvent == null)
            {
                if (externalBuisnessCheckFunc == null)
                {
                    personAllowed = false;
                    reason = "دسترسی عبور برای فرد مورد نظر تعریف نشده است!";
                }
                else //check external buisness only if person event is not declared
                    personAllowed = externalBuisnessCheckFunc(person.PersonRefNumber, pkEventType, pkEvent, out reason);
            }
            else if (!personEvent.IsAllowed)
            {
                personAllowed = false;
                reason = personEvent.AccessReason;
            }
            else if (!person.IsPersonExpireDateNull() && person.PersonExpireDate < DateTime.Now)
            {
                personAllowed = false;
                reason = "مهلت دسترسی میهمان به پایان رسیده است";
            }
            else if ((!personEvent.IsAccessStartNull() && DateTime.Now < personEvent.AccessStart) ||
                                (!personEvent.IsAccessEndNull() && DateTime.Now > personEvent.AccessEnd))
            {
                personAllowed = false;
                reason = "دسترسی در خارج از محدوده زمانی معین";
            }

            return personAllowed;
        }

        public static AccessControlDS.tblPersonEventDataTable SelectAll()
        {
            return PersonEventAccess.SelectAll();
        }


        public static AccessControlDS.tblPersonEventDataTable SelectByFilter(String filter)
        {
            return PersonEventAccess.SelectByFilter(filter);
        }


        public static void Insert(AccessControlDS.tblPersonEventRow eventLogRow)
        {
            PersonEventAccess.Insert(eventLogRow);
        }

        public static void Update(AccessControlDS.tblPersonEventRow eventLogRow)
        {
            PersonEventAccess.Update(eventLogRow);
        }

        public static void Delete(AccessControlDS.tblPersonEventRow row)
        {
            PersonEventAccess.Delete(row);
        }    
        
        public static void DeleteByFKPerson(String fkPerson)
        {
            var lastPersonEvent =
              SelectByFilter(String.Format("fkPerson LIKE'{0}'", fkPerson));
            if(lastPersonEvent==null)
                return;
            foreach (var personEventRow in lastPersonEvent)
            {
                Delete(personEventRow);
            }
        }


    }
}
